package code.whmmm.util.collection;

import java.util.LinkedList;

/**
 * <p><b>
 * 字符 list
 * </b></p>
 * <br/>
 * 创建时间: 2019/9/30 16:08
 *
 * @author Zero
 */
public class CharList {

    private LinkedList<Character> list = new LinkedList<Character>();

    /**
     * 将 {@link String} 打散成 Character list
     *
     * @param s {@link String}
     */
    public CharList(String s) {

        for (int i = 0; i < s.length(); i++) {
            list.add(s.charAt(i));
        }
    }

    /**
     * 从头部开始移除字符
     *
     * @param length 移除的长度
     * @return {@link String}
     */
    public String removeAndGet(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(list.remove());
        }

        return sb.toString();
    }

    /**
     * 向尾部追加
     *
     * @param s
     * @return
     */
    public CharList append(String s) {
        for (int i = 0; i < s.length(); i++) {
            list.add(s.charAt(i));
        }
        return this;
    }

    /**
     * 向尾部追加
     *
     * @param c
     * @return
     */
    public CharList append(char c) {
        list.add(c);
        return this;
    }

    public int size() {
        return list.size();
    }

    public String value() {
        StringBuilder sb = new StringBuilder();
        for (Character character : list) {
            sb.append(character);
        }
        return sb.toString();
    }

    public LinkedList<Character> getList() {
        return list;
    }


    /**
     * 向头部追加
     *
     * @param s 字符串
     * @return this
     */
    public CharList prepend(String s) {
        for (int i = s.length() - 1; i >= 0; i--) {
            list.offerFirst(s.charAt(i));
        }
        return this;
    }

    /**
     * 向头部追加
     *
     * @param c 字符
     * @return this
     */
    public CharList prepend(char c) {
        list.offerFirst(c);
        return this;
    }


    /*public static void main(String[] args) {
        CharList list = new CharList("123");

        list.prepend("abc");

        System.out.println(list);
    }*/
}
